package cn.xuguowen.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * ClassName: SpringContext
 * Package: cn.xuguowen.util
 * Description:
 *
 * @Author 徐国文
 * @Create 2023/3/12 11:57
 * @Version 1.0
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 根据类型获取Bean实例
     * @param type
     * @return
     */
    public static Object getBean(Class type) {
        Object bean =  applicationContext.getBean(type);
        return bean;
    }
}
